package com.platform.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.jfinal.aop.Duang;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.wxaapp.WxaConfig;
import com.jfinal.wxaapp.WxaConfigKit;
import com.jfinal.wxaapp.api.WxaTemplate;
import com.jfinal.wxaapp.api.WxaTemplateApi;
import com.platform.dao.*;
import com.platform.entity.*;
import com.platform.service.OrderService;
import com.platform.utils.R;
import com.platform.utils.ResourceUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    //订单发货提醒
    public final static String OrderDeliveryReminder = "4JwepfhXfbDAJoM9qCEtEoFOAd3VNIPsiJlLPjLxlEU";

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderGoodsDao orderGoodsDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ShippingDao shippingDao;
    @Autowired
    private WxFromidDao wxFromidDao;
    @Resource
    private RabbitTemplate amqpTemplate;

    @Override
    public OrderEntity queryObject(Integer id) {
        return orderDao.queryObject(id);
    }

    @Override
    public List<OrderEntity> queryList(Map<String, Object> map) {
        return orderDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return orderDao.queryTotal(map);
    }

    @Override
    public int save(OrderEntity order) {
        return orderDao.save(order);
    }

    @Override
    public int update(OrderEntity order) {
        return orderDao.update(order);
    }

    @Override
    public int delete(Integer id) {
        return orderDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return orderDao.deleteBatch(ids);
    }

    @Override
    public R confirm(Integer id) {
        OrderEntity orderEntity = queryObject(id);
        Integer shippingStatus = orderEntity.getShippingStatus();//发货状态
        Integer payStatus = orderEntity.getPayStatus();//付款状态
        if (2 != payStatus) {
            return R.error("此订单未付款，不能确认收货！");
        }
        if (4 == shippingStatus) {
            return R.error("此订单处于退货状态，不能确认收货！");
        }
        if (0 == shippingStatus || orderEntity.getOrderStatus() != 300) {
            return R.error("此订单未发货，不能确认收货！");
        }

        orderEntity.setOrderStatus(301);//301-用户确认收货
        orderEntity.setShippingStatus(2);//0-未发货；1-已发货；2-已经收货；4-退货
        orderEntity.setConfirmTime(new Date());//确认收货时间

        orderDao.update(orderEntity);

        return R.ok();
    }

    @Override
    public R sendGoods(OrderEntity order) {
        Integer payStatus = order.getPayStatus();//付款状态
        if (2 != payStatus) {
            return R.error("此订单未付款！");
        }

        if (Validator.isEmpty(order.getShippingId())) {
            return R.error("请选择快递公司");
        }

        if (Validator.isEmpty(order.getShippingNo())) {
            return R.error("请填写快递单号");
        }


        ShippingEntity shippingEntity = shippingDao.queryObject(order.getShippingId());
        if (shippingEntity == null) {
            return R.error("请选择快递公司");
        }
        order.setShippingName(shippingEntity.getName());
        order.setOrderStatus(300);//订单已发货
        order.setShippingStatus(1);//已发货
        order.setDeliveryTime(new Date());
        orderDao.update(order);

        //发送队列信息
        Map<String, Object> item = new HashMap<>();
        item.put("orderId", order.getId());
        amqpTemplate.convertAndSend("test_rmq_exchange", "order_sendGoods_queue_patt", item);

        return R.ok();
    }

    @Override
    public BigDecimal checkActualPrice(Long user_id) {
        return orderDao.checkActualPrice(user_id);
    }
}
